//
//  PinYinView.m
//  ToolLibrary
//
//  Created by 蓓蕾 on 2024/1/22.
//  Copyright © 2024 Yang. All rights reserved.
//

#import "PinYinView.h"
#import "Masonry.h"

@interface PinYinView ()

@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, strong) NSArray *pinYinArray;

@property (nonatomic, assign) double viewMaxWidth;

@property (nonatomic, strong) PinYinConfig *config;

@end

@implementation PinYinView

+(instancetype)createTextViewWidth:(CGFloat)width contentArray:(NSArray *)contentArray pinYinArray:(NSArray *)pinYinArray config:(PinYinConfig *)config withEndBlock:(void(^)(PinYinView *endResult))endResult {
    PinYinView *readView = [[PinYinView alloc] init];
    readView.contentArray = contentArray;
    readView.pinYinArray = pinYinArray;
    readView.viewMaxWidth = width;
    readView.config = config;
    readView.CreateFinishedBlock = [endResult copy];
    [readView setup];
    return readView;
}
+(instancetype)createTextViewWidth:(CGFloat)width contentArray:(NSArray *)contentArray pinYinArray:(NSArray *)pinYinArray config:(PinYinConfig *)config {
    PinYinView *readView = [[PinYinView alloc] init];
    readView.contentArray = contentArray;
    readView.pinYinArray = pinYinArray;
    readView.viewMaxWidth = width;
    readView.config = config;
    [readView setup];
    return readView;
}

+(instancetype)createTextViewWidth:(CGFloat)width contentArray:(NSArray *)contentArray pinYinArray:(NSArray *)pinYinArray {
    PinYinView *readView = [[PinYinView alloc] init];
    readView.contentArray = contentArray;
    readView.pinYinArray = pinYinArray;
    readView.viewMaxWidth = width;
    [readView setup];
    return readView;
}

+(instancetype)createTextViewWidth:(CGFloat)width contentArray:(NSArray *)contentArray pinYinArray:(NSArray *)pinYinArray withEndBlock:(void(^)(PinYinView *endResult))endResult{
    PinYinView *readView = [[PinYinView alloc] init];
    readView.contentArray = contentArray;
    readView.pinYinArray = pinYinArray;
    readView.viewMaxWidth = width;
    readView.CreateFinishedBlock = [endResult copy];
    [readView setup];
    return readView;
}

-(void)setup {
    UIView *lastView;
    UIView *lastLineView;
    CGFloat totalWidth = 0;
    CGFloat totalHeight = 0;
    CGFloat needChangeLine = NO;
    NSInteger line = 0;
    for (NSInteger i = 0; i < _contentArray.count; ++i) {
        NSString *pinYinString = @"";
        NSString *wordString = _contentArray[i];
        if ([wordString isEqualToString:@"\n"] && i != 0) {
            totalWidth = 0;
            line++;
            needChangeLine = YES;
            lastLineView = lastView;
            lastView = nil;
            continue;
        }
        if (i < _pinYinArray.count) {
            pinYinString = _pinYinArray[i];
        }
        UIFont *pinYinFont = [UIFont systemFontOfSize:12];
        UIFont *wordFont = [UIFont systemFontOfSize:12];
        CGFloat userSetItemViewWidth = 0;
        if (_config) {
            pinYinFont = _config.pinYinFont ? _config.pinYinFont : (_config.font ? _config.font : [UIFont systemFontOfSize:12]);
            wordFont = _config.wordFont ? _config.wordFont : (_config.font ? _config.font : [UIFont systemFontOfSize:12]);
            if (_config.viewSize) {
                NSValue *viewSizeValue = _config.viewSize;
                CGSize size = [viewSizeValue CGSizeValue];
                userSetItemViewWidth = size.width;
            } else if (_config.itemWidth > 0) {
                userSetItemViewWidth = _config.itemWidth;
            }
        }
        CGFloat wordWidth = [wordString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:wordFont} context:nil].size.width+0.5;
        CGFloat pinYinWidth = [pinYinString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:pinYinFont} context:nil].size.width+0.5;
//        CGFloat wordWidth = [wordString yy_maxWidthWithFont:wordFont Height:20];
//        CGFloat pinYinWidth = [pinYinString yy_maxWidthWithFont:wordFont Height:20];
        CGFloat itemViewWidth = userSetItemViewWidth > 0 ? userSetItemViewWidth : MAX(wordWidth, pinYinWidth);
        

        
//        NSString *wordResult = wordString.length > 0 ? wordString : pinYinString;
//        NSString *pinYinResult = pinYinString.length > 0 ? pinYinString : wordString;
//        CGFloat wordHeight = [wordResult yy_maxHeightWithFont:wordFont Width:MAXFLOAT];
//        CGFloat pinYinHeight = [pinYinResult yy_maxHeightWithFont:pinYinFont Width:MAXFLOAT];
//        CGFloat itemViewHeight = wordHeight + pinYinHeight + 2;
        
        PinYinItemView *itemView = [[PinYinItemView alloc] initWithConfig:_config];
        [self addSubview:itemView];
        itemView.pinYinString = pinYinString;
        itemView.wordString = wordString;
        
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (needChangeLine && lastLineView) {
                if (lastView) {
                    make.left.mas_equalTo(lastView.mas_right).mas_offset(5);
                } else {
                    make.left.mas_equalTo(self.mas_left).mas_offset(0);
                }
                make.top.mas_equalTo(lastLineView.mas_bottom).mas_offset(5);
            } else {
                if (lastView) {
                    make.top.mas_equalTo(lastView.mas_top).mas_offset(0);
                    make.left.mas_equalTo(lastView.mas_right).mas_offset(5);
                } else {
                    make.top.mas_equalTo(self.mas_top).mas_offset(0);
                    make.left.mas_equalTo(self.mas_left).mas_offset(0);
                }
            }
            make.right.mas_lessThanOrEqualTo(self.mas_right).mas_offset(0);
            make.width.mas_equalTo(itemViewWidth);
        }];
        lastView = itemView;
        
        if (i == 0) {
            totalWidth += itemViewWidth;
        } else {
            totalWidth += (itemViewWidth + 5);
        }
        if (lastLineView) {
            lastLineView = nil;
        }
        if (i + 1 < _contentArray.count) {
            NSString *pinYinString = @"";
            NSString *wordString = _contentArray[i+1];
            if (i+1 < _pinYinArray.count) {
                pinYinString = _pinYinArray[i+1];
            }
            CGFloat wordWidth = [wordString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:wordFont} context:nil].size.width+0.1;
            CGFloat pinYinWidth = [pinYinString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:pinYinFont} context:nil].size.width+0.1;
            CGFloat itemViewWidth = MAX(wordWidth, pinYinWidth);
            
            if (totalWidth + itemViewWidth > _viewMaxWidth) {//换行
                totalWidth = 0;
                line++;
                needChangeLine = YES;
                lastLineView = itemView;
                lastView = nil;
            } else {//不换行
                needChangeLine = NO;
            }
        } else {
            if (lastView) {
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_greaterThanOrEqualTo(lastView.mas_bottom).mas_offset(0);
                }];
            }
            
        }
    
        
//        if (line % 2 == 0) {
//            itemView.backgroundColor = [UIColor orangeColor];
//        } else {
//            itemView.backgroundColor = [UIColor purpleColor];
//        }
        
    }
    NSLog(@"totalWidth = %lf",totalWidth);
    NSLog(@"totalHeight = %lf",totalHeight);
    self.realWidth = totalWidth;
    self.realHeight = totalHeight;
//    self.backgroundColor = [UIColor cyanColor];
    if (self.CreateFinishedBlock) {
        self.CreateFinishedBlock(self);
    }
}
//-(CGFloat)layoutListNameLength:(NSString *)content{
//
//
//
//    CGFloat width = [content boundingRectWithSize:CGSizeMake(_viewMaxWidth, ([UIFont systemFontOfSize:12].pointSize+5)*2) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kFont} context:nil].size.width+8;
//    return width;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
